<?php
include_once 'library/Grafik.class.php';

class RencanaPenanamanModel{
	private $db;
	private $subbagian;
	
	private $year;
	private $chart;
	private $charg_file;
	private $chart_type;
	private $chart_value;

	private $validColumn;
	private $data;
	
	function RencanaPenanamanModel($dbObject){
		$this->subbagian 	= 'rencana_penanaman';		
		$this->db 			= $dbObject;
		
		// init param
		$this->year 	  = $this->getGET('year', date('Y', time()));
		$this->chart_type = $this->getGET('chart_type', 'bars');
		
		// create chart  handler
		$this->chart = new Grafik();
		
		$this->validColumn  = array(
			'tahun', 
			'bulan_1', 
			'bulan_2',
			'bulan_3', 
			'bulan_4',
			'bulan_5',
			'bulan_6',
			'bulan_7',
			'bulan_8',
			'bulan_9',
			'bulan_10',
			'bulan_11',
			'bulan_12'
			);
	}
	
	function format($data){
		$result = array();
		foreach($this->validColumn as $v){
			if( isset($data[$v]) )
				$result[$v] = $data[$v];
			else
				$result[$v] = NULL;
		}
		
		return $result;
	}
	
	private function insertRencanaPenanaman(){
		// delete all
		$q = "DELETE FROM `caramel_operational`.`penanaman_rencana` WHERE tahun='".$this->data['tahun']."'";
		$this->db->query($q);
		
		
		// then insert new one
		for($bulan=1; $bulan<=12; $bulan++){
			$query = "INSERT INTO `caramel_operational`.`penanaman_rencana` (`tahun`, `bulan`, `jumlah`) VALUES ('".$this->data['tahun']."','$bulan', '".$this->data['bulan_'.$bulan]."')";
			$this->db->query($query);
		}
	}
	
	public function listYear(){
		$q = 'SELECT DISTINCT YEAR(`tgl_penanaman`) as tahun FROM `caramel_operational`.`penanaman` ORDER BY tahun ASC';
		
		return $this->db->get_results($q, ARRAY_A);
	}
	
	private function getGET($param, $defaut=NULL){
		if( isset( $_GET[$param] ) && $_GET[$param] != '' )
			return $_GET[$param];
		else
			return $defaut;
	}
	
	public function put($data){
		$this->data	= $this->format($data);// buang field2 yang tidak dibutuhkan
		
		if( $this->isExist() == FALSE ){				
			// inventaris
			$this->insertRencanaPenanaman();					
		}
		else{
			return INSERT_FAIL_DUPLICATE;
		}		
	}
	
	private function isExist(){			
		return FALSE; // tidak ada data yang dicari... no duplicate		
	}
	
	public function generateChart(){
		$this->search();
		$this->phplot2amplot();
	}
	
	private function phplot2amplot(){ // phplot to amplot chart data type
		$chart_title = 'Rencana Vs Realiasasi Penanaman <br/>'.$this->year;
		$xml_file = 'include/chart_data/rencana_penanaman.xml';
		$xml_data = '   <?xml version="1.0" encoding="UTF-8"?>';
		$xml_data.= '	<chart>';
		$xml_data.= '		<series>';		
		$i=0;
		foreach($this->chart_value as $c){
			$series_name = $c[0];
			$xml_data.= '		<value xid="'.$i++.'">'.$series_name.'</value>';
		}		
		$xml_data.= '		</series>';
		
		$xml_data.= '		<graphs>';
		
		$xml_data.= '			<graph gid="1">'; // data rencana		
		$i=0;
		foreach($this->chart_value as $c){
			$series_value = $c[2];
			$xml_data.= '			<value xid="'.$i++.'">'.$series_value.'</value>';
		}			
		$xml_data.= '			</graph>';
		
		$xml_data.= '			<graph gid="2">'; // data rencana		
		$i=0;
		foreach($this->chart_value as $c){
			$series_value = $c[1];
			$xml_data.= '			<value xid="'.$i++.'">'.$series_value.'</value>';
		}			
		$xml_data.= '			</graph>';
		
		$xml_data.= '		</graphs>';
		$xml_data.= '       <labels>'."\n";
		$xml_data.= '           <label lid="2">'."\n";
		$xml_data.= '                <x>0</x>'."\n";
		$xml_data.= '                <y>25</y>'."\n";
		$xml_data.= '                <align>center</align>'."\n";
		$xml_data.= '                <text><![CDATA[<b>'.$chart_title.'</b>]]></text>'."\n";
		$xml_data.= '           </label>'."\n";
		$xml_data.= '       </labels>'."\n";
		$xml_data.= '	</chart>';
		
		file_put_contents($xml_file, $xml_data);
	}
	
	public function search(){		
		// nilai real
		for($i=0; $i<12; $i++){
			$query[]= sprintf("
				SELECT COUNT(*) FROM `caramel_operational`.`penanaman`
				WHERE YEAR(tgl_penanaman)='$this->year'
				AND MONTH(tgl_penanaman)='%02d'", $i+1);
		}
	
		// nilai rencana
		for($i=0; $i<12; $i++){
			$query_rencana[]= "
				SELECT `jumlah` FROM `caramel_operational`.`penanaman_rencana`
				WHERE `tahun`='$this->year'
				AND `bulan`='".($i+1)."'";
		}
		
		$x	= array(
			"Januari","Februari","Maret","April","Mei","Juni",
			"Juli","Agustus","September","Oktober","November","Desember");
		$y 	= array();
		foreach($query as $q){
			$temp = $this->db->get_var($q);
			if(empty($temp)) $y[] = 0;
			else $y[] = $temp;
		} 
		
		$y_rencana 	= array();
		foreach($query_rencana as $q){
			$temp = $this->db->get_var($q);
			if(empty($temp)) $y_rencana[] = 0;
			else $y_rencana[] = $temp;
		}
	
		$data = array();
		for($i=0; $i<count($x); $i++){
			$data[$i] = array($x[$i], $y[$i], $y_rencana[$i]);
		}
		$this->chart_value  = $data;
	}
	
	public function getChartValue(){
		return $this->chart_value;
	}

}
?>